<?php

namespace App\Admin\Actions\Staff;

use App\Library\Logic\OperateLogLogic;
use App\Models\ShopUserModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class StaffDataShareAction extends RowAction
{
    public function __construct(int $status = 0)
    {
        parent::__construct();
        $this->name = $status === 2 ? '开启共享' : '取消共享';
    }

    public function handle(ShopUserModel $model, Request $request): Response
    {
        $power = (int)$model->user_peer_power === 2 ? 1 : 2;
        if ((int)$model->user_delete_flag === 1) return $this->response()->error(__('errorCode.433'))->refresh();
        $model->user_peer_power = $power;
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.staff.dataShare', ['userId' => $model->user_id, 'userPeerPower' => $power]);
        return $this->response()->success('操作成功')->refresh();
    }
}
